home *** CD-ROM | disk | FTP | other *** search
/ Champak 26 (Anniversary Edition) / Volume 26 [Anniversary Edition] - JOGO DISK .iso / DEPOSITO / REmovido / Esportes / 814.swf / scripts / CPuck.as < prev    next >
Text File  |  2006-06-13  |  5KB  |  142 lines

  1. function CPuck()
  2. {
  3. }
  4. function VectorSize(vec)
  5. {
  6.    return Math.sqrt(vec.x * vec.x + vec.y * vec.y);
  7. }
  8. CPuck.prototype = new MovieClip();
  9. Object.registerClass("CPuck",CPuck);
  10. CPuck.prototype.GoIdle = function(really)
  11. {
  12.    this._idle = really;
  13. };
  14. CPuck.prototype.onEnterFrame = function()
  15. {
  16.    var motionSegment = new Object();
  17.    var timeWithinFrame = 0;
  18.    if(!this._velX || !this._velY)
  19.    {
  20.       return undefined;
  21.    }
  22.    while(timeWithinFrame < 1)
  23.    {
  24.       motionSegment.origin = new Object();
  25.       motionSegment.origin.x = this._x;
  26.       motionSegment.origin.y = this._y;
  27.       motionSegment.direction = new Object();
  28.       motionSegment.direction.x = this._velX;
  29.       motionSegment.direction.y = this._velY;
  30.       var collisionInfo = this.HitPaddle(motionSegment);
  31.       if(collisionInfo == null)
  32.       {
  33.          collisionInfo = this.HitHump(motionSegment);
  34.          if(collisionInfo != null)
  35.          {
  36.             this._parent.BallHitHump();
  37.          }
  38.       }
  39.       else
  40.       {
  41.          this._parent.BallHit();
  42.       }
  43.       if(collisionInfo != null)
  44.       {
  45.          timeWithinFrame += (1 - timeWithinFrame) * collisionInfo.touchTime;
  46.          var mirroredVel = MirrorVector({x:this._velX,y:this._velY},collisionInfo.touchAngle / 180 * 3.141592653589793);
  47.          this._velX = - mirroredVel.x;
  48.          this._velY = - mirroredVel.y;
  49.          motionSegment.origin = collisionInfo.touchPoint;
  50.          motionSegment.direction.x = this._velX * (1 - timeWithinFrame);
  51.          motionSegment.direction.y = this._velY * (1 - timeWithinFrame);
  52.       }
  53.       else
  54.       {
  55.          timeWithinFrame = 1;
  56.          this._x = motionSegment.origin.x + motionSegment.direction.x;
  57.          this._y = motionSegment.origin.y + motionSegment.direction.y;
  58.       }
  59.    }
  60.    if(this.IsOutOfBounds())
  61.    {
  62.       this._parent.PuckOutOfBounds();
  63.    }
  64. };
  65. CPuck.prototype.HitHump = function(motionSegment)
  66. {
  67.    var humpCircle = new Object();
  68.    humpCircle.center = new Object();
  69.    humpCircle.center.x = this._parent._hump._x;
  70.    humpCircle.center.y = this._parent._hump._y;
  71.    humpCircle.radius = this._parent._hump._width * 0.5 + 4;
  72.    var firstIntersectionInfo = FindSegmentCircleIntersections(motionSegment,humpCircle);
  73.    if(firstIntersectionInfo)
  74.    {
  75.       var collisionInfo = new Object();
  76.       collisionInfo.touchPoint = firstIntersectionInfo.touchPoint;
  77.       var deltaX = humpCircle.center.x - collisionInfo.touchPoint.x;
  78.       var deltaY = humpCircle.center.y - collisionInfo.touchPoint.y;
  79.       collisionInfo.touchAngle = Math.atan2(deltaY,deltaX) / 3.141592653589793 * 180;
  80.       collisionInfo.touchTime = firstIntersectionInfo.touchTime;
  81.       return collisionInfo;
  82.    }
  83.    return null;
  84. };
  85. CPuck.prototype.HitPaddle = function(motionSegment)
  86. {
  87.    var earliestCollisionInfo = null;
  88.    var destination = new Object();
  89.    destination.x = motionSegment.origin.x + motionSegment.direction.x;
  90.    destination.y = motionSegment.origin.y + motionSegment.direction.y;
  91.    if(!this._idle && VectorSize(destination) >= 124)
  92.    {
  93.       iPaddle = 1;
  94.       while(iPaddle <= this._parent._nPaddles)
  95.       {
  96.          var paddle = this._parent["paddle" + iPaddle];
  97.          var arc = new Object();
  98.          arc.center = new Object();
  99.          arc.center.x = 0;
  100.          arc.center.y = 0;
  101.          arc.radius = 124;
  102.          arc.angularMidpoint = paddle._rotation;
  103.          arc.arcLength = paddle._arcLength;
  104.          var containsOrigin = ArcContainsPoint(arc,motionSegment.origin);
  105.          var containsDest = ArcContainsPoint(arc,destination);
  106.          if(!(!containsOrigin && !containsDest))
  107.          {
  108.             var firstIntersectionInfo = FindSegmentArcIntersections(motionSegment,arc);
  109.             if(firstIntersectionInfo)
  110.             {
  111.                var collisionInfo = new Object();
  112.                collisionInfo.touchAngle = WrapAngle(paddle._rotation);
  113.                collisionInfo.touchPoint = firstIntersectionInfo.touchPoint;
  114.                collisionInfo.touchTime = firstIntersectionInfo.touchTime;
  115.                if(!earliestCollisionInfo || collisionInfo.touchTime < earliestCollisionInfo.touchTime)
  116.                {
  117.                   earliestCollisionInfo = collisionInfo;
  118.                }
  119.             }
  120.          }
  121.          iPaddle++;
  122.       }
  123.    }
  124.    return earliestCollisionInfo;
  125. };
  126. CPuck.prototype.MoveInsideEdge = function()
  127. {
  128.    while(this.DistanceFromCenter() > 118)
  129.    {
  130.       this._x -= this._velX;
  131.       this._y -= this._velY;
  132.    }
  133. };
  134. CPuck.prototype.DistanceFromCenter = function()
  135. {
  136.    return Math.sqrt(this._x * this._x + this._y * this._y);
  137. };
  138. CPuck.prototype.IsOutOfBounds = function()
  139. {
  140.    return !this._idle ? this.DistanceFromCenter() > 142 : false;
  141. };
  142.